package 算法.leetcode.labuladong.数组和链表;

import 算法.leetcode.offer.base.ListNode;

/**
 * @author lchenglong
 * @date 2022/6/15
 */
public class 合并两个有序链表 {

    class Solution {

        public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
            ListNode dummy = new ListNode(-1);
            ListNode cursor = dummy;
            while (list1 != null && list2 != null) {
                if (list1.val > list2.val) {
                    cursor.next = list1;
                    list1 = list1.next;
                } else {
                    cursor.next = list2;
                    list2 = list2.next;
                }
                // 游标后移
                cursor = cursor.next;
            }
            if (list1!=null){
                cursor.next = list1;
            }
            if (list2!=null) {
                cursor.next = list2;
            }
            return dummy.next;
        }
    }
}
